/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} root
 * @param {TreeNode} p
 * @param {TreeNode} q
 * @return {TreeNode}
 */
// 最差时间复杂度访问所有节点，最差空间复杂度递归N次
// O(N) O(N)
var lowestCommonAncestor = function (root, p, q) {
    if (root == null || root == p || root == q) {
        return root
    }
    let left = lowestCommonAncestor(root.left, p, q)
    let right = lowestCommonAncestor(root.right, p, q)
    if (left === null && right === null) return null;
    if (left == null) {
        return right
    }
    if (right == null) {
        return left
    }
    return root
};